﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

using SBPweb.RIA.Client.Contracts.Base;
using SBPweb.RIA.Client.Controls.Windows;

using SBPweb.PROvibe.Client.Library.Base;
using SBPweb.PROvibe.Client.Library.Models;
using SBPweb.PROvibe.Shared.Library.Layout;

namespace SBPweb.PROvibe.Client.Core.Controls
{

    public partial class PageManager : PageBase
    {

        #region Fields

        private bool? isClosable = false;

        #endregion

        #region Constructor logic

        public PageManager()
        {
            InitializeComponent();
            InitializeHandlers();
        }

        #endregion

        #region Methods

        protected override void OnLoaded(object sender, RoutedEventArgs e)
        {
            base.OnLoaded(sender, e);

            ClientContext.ContentContextController.GetCurrentPageContext();

            cDesktop.Visibility = ClientContext.ContentContextModel.Layout == LayoutModes.Full ? Visibility.Visible : Visibility.Collapsed;
            cModuleBar.Visibility = ClientContext.ContentContextModel.Layout == LayoutModes.Full ? Visibility.Visible : Visibility.Collapsed;
            cPowerButton.Visibility = ClientContext.ContentContextModel.Layout == LayoutModes.Full ? Visibility.Visible : Visibility.Collapsed;
        }

        protected override void InitializeHandlers()
        {
            base.InitializeHandlers();

            if (Application.IsRunningOutOfBrowser)
            {
                //Application.MainWindow.Closing += new EventHandler<ClosingEventArgs>(MainWindow_Closing);
            }
        }
        
        protected override void FinalizeHandlers()
        {
            if (Application.IsRunningOutOfBrowser)
            {
                //Application.MainWindow.Closing -= new EventHandler<ClosingEventArgs>(MainWindow_Closing);
            }

            base.FinalizeHandlers();
        }

        internal void ShowCloseConfirmationWindow()
        {
            MessageWindow mw = new MessageWindow();
            mw.Title = "Kilépés...";
            mw.Header = "Kilépne a PROvibe-ból?";
            mw.ChromeBrush = new SolidColorBrush(Color.FromArgb(208, 255, 255, 255));
            mw.MinWidth = 300;
            mw.MinHeight = 120;
            mw.Margin = new Thickness(5);
            TextBlock tb = new TextBlock();
            tb.Text = "Amennyiben az Ok gombra kattint, az alkalmazás befejezi működését...";
            tb.TextWrapping = TextWrapping.Wrap;
            mw.Body = tb;
            mw.HasOkButton = true;
            mw.HasCancelButton = true;
            mw.OpenDialog();
            mw.Closed += new EventHandler(MessageWindow_Closed);
        }

        #endregion

        #region Event handlers

        //private void MainWindow_Closing(object sender, ClosingEventArgs e)
        //{
        //    if (isClosable.HasValue && isClosable.Value)
        //    {
        //        return;
        //    }
        //    e.Cancel = true;
        //    ShowCloseConfirmationWindow();
        //}

        private void MessageWindow_Closed(object sender, EventArgs e)
        {
            MessageWindow mw = sender as MessageWindow;
            if (mw.DialogResult.HasValue && mw.DialogResult.Value)
            {
                if (!isClosable.HasValue || !isClosable.Value)
                {
                    isClosable = true;
                }
                if (Application.IsRunningOutOfBrowser && Application.HasElevatedPermissions)
                {
                    Application.MainWindow.Close();
                }
                else
                {
                    HtmlPage.Window.Eval("close()");
                }
            }
        }

        #endregion
        
    }

}